package top.fyxxxx.dbdesign.util;

import top.fyxxxx.dbdesign.dao.DBoperate;
import top.fyxxxx.dbdesign.data.Account;
import top.fyxxxx.dbdesign.data.Course;
import top.fyxxxx.dbdesign.data.PersonalData;
import top.fyxxxx.dbdesign.data.SignInTask;

import java.text.SimpleDateFormat;
import java.util.*;

public class Manager implements CourseManager,TaskManager{
    private static Manager instance=new Manager();
    DBoperate dBoperate=DBoperate.getInstance();
    private Manager(){}
    public static CourseManager courseManager(){return instance;}
    public static TaskManager taskManager(){return instance;}

    @Override
    public long createCourse(long uid, String name) {
        Account acc=dBoperate.getAccountByUid(uid);
        //权限检测
        if(acc.getPower()==0)return -1;
        long time=System.currentTimeMillis();
        Course course=new Course(name,time,time,uid,null,null,null);
        dBoperate.insertCourse(course);
        PersonalData pd=dBoperate.getPersonalData(uid,time);
        pd.setJoinTime(time);pd.setPower(3);
        dBoperate.joinCourse(pd);
        return time;
    }

    @Override
    public boolean destroyCourse(long uid, long cid) {
        Course course=dBoperate.getCourse(cid);
        if(course.getCreateTime()==0)return true;
        //权限检查
        PersonalData pd=dBoperate.getPersonalData(uid,cid);
        if((pd.getPower()&1)==0)return false;
        dBoperate.deleteCourse(cid);
        return true;
    }

    @Override
    public boolean joinCourse(long uid, long cid) {
        PersonalData pd=dBoperate.getPersonalData(uid,cid);
        if(pd.getJoinTime()!=0)return true;
        pd.setJoinTime(System.currentTimeMillis());pd.setPower(0);
        dBoperate.joinCourse(pd);
        return true;
    }

    @Override
    public boolean exitCourse(long uid, long cid) {
        //权限检查
        PersonalData pd=dBoperate.getPersonalData(uid,cid);
        if((pd.getPower()&1)!=0)return false;
        dBoperate.exitCourse(uid,cid);
        return true;
    }

    @Override
    public boolean setAdmin(long uid, long who, long cid) {
        PersonalData pd=dBoperate.getPersonalData(uid,cid);
        if((pd.getPower()&1)==0)return false;//权限检查
        pd=dBoperate.getPersonalData(who,cid);
        if(pd.getJoinTime()==0)return false;//不在课程内
        pd.setPower(pd.getPower()|2);
        dBoperate.updateJoinCourse(pd);
        return true;
    }

    @Override
    public boolean removeAdmin(long uid, long who, long cid) {
        PersonalData pd=dBoperate.getPersonalData(uid,cid);
        if((pd.getPower()&1)==0)return false;//权限检查
        pd=dBoperate.getPersonalData(who,cid);
        if(pd.getJoinTime()==0)return false;//不在课程内
        pd.setPower(pd.getPower()&(~2));
        dBoperate.updateJoinCourse(pd);
        return true;
    }

    @Override
    public List<Course> searchCourse(String exp) {
        List<Course> ret=new ArrayList<>();
        for(long cid:dBoperate.searchCourse(exp)){
            ret.add(dBoperate.getCourse(cid));
        }
        return ret;
    }

    @Override
    public List<Course> getPowerCourse(long uid) {
        List<Course>ret=new ArrayList<>();
        for(long cid:dBoperate.getJoinCids(uid)){
            PersonalData pd=dBoperate.getPersonalData(uid,cid);
            if((pd.getPower()&2)!=0)ret.add(dBoperate.getCourse(cid));
        }
        return ret;
    }

    @Override
    public long createTask(long uid, long beginTime, long endTime, long belongToCid) {
        PersonalData pd=dBoperate.getPersonalData(uid,belongToCid);
        if((pd.getPower()&2)==0)return 0;//无权限
        long time=System.currentTimeMillis();
        dBoperate.insertSignInTask(new SignInTask(time,uid,time,beginTime,endTime,belongToCid));
        return time;
    }

    @Override
    public boolean destroyTask(long uid, long sid) {
        SignInTask task=dBoperate.getSignInTask(sid);
        if(task.getBelongToCid()==0)return false;//不存在
        PersonalData pd=dBoperate.getPersonalData(uid,task.getBelongToCid());
        if((pd.getPower()&2)==0)return false;//无权限
        dBoperate.deleteSignInTask(sid);
        return true;
    }

    @Override
    public boolean modifyTask(long uid, long sid, long beginTime, long endTime) {
        SignInTask task=dBoperate.getSignInTask(sid);
        if(task.getBelongToCid()==0)return false;//不存在
        PersonalData pd=dBoperate.getPersonalData(uid,task.getBelongToCid());
        if((pd.getPower()&2)==0)return false;//无权限
        task.setBeginTime(beginTime);task.setEndTime(endTime);
        dBoperate.updateSignInTask(task);
        return true;
    }

    @Override
    public List<SignInTask> getCurTask(long uid) {
        List<SignInTask>ret=new ArrayList<>();
        Set<Long> cidSet=dBoperate.getJoinCids(uid);
        for(long cid:cidSet)
            for(long sid:dBoperate.searchSignInTask(System.currentTimeMillis(),cid)){
                ret.add(dBoperate.getSignInTask(sid));
            }
        return ret;
    }

    @Override
    public List<String> getTimes(long sid) {
        SignInTask task=dBoperate.getSignInTask(sid);
        SimpleDateFormat formatter=new SimpleDateFormat ("yyyy MM dd HH mm");
        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date curDate=new Date(task.getBeginTime());//获取当前时间
        String[]str=formatter.format(curDate).split(" ");
        List<String> ret = new ArrayList<>(Arrays.asList(str));
        curDate=new Date(task.getEndTime());
        str=formatter.format(curDate).split(" ");
        ret.add(str[3]);ret.add(str[4]);
        return ret;
    }
}
